import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * 主菜单面板
 */
public class MainMenuPanel extends JPanel {
    private HotelManagementSystem mainFrame;
    private JPanel contentPanel;
    private CardLayout contentLayout;
    private JLabel statusLabel;

    // 功能面板
    private FrontDeskPanel frontDeskPanel;
    private ReservationPanel reservationPanel;
    private GuestInfoPanel guestInfoPanel;
    private HistoryPanel historyPanel;
    private RoomStatusPanel roomStatusPanel;

    public MainMenuPanel(HotelManagementSystem mainFrame) {
        this.mainFrame = mainFrame;
        setupUI();
        initializePanels();
    }

    private void setupUI() {
        setLayout(new BorderLayout());
        setBackground(HotelManagementSystem.getBackgroundColor());

        // 顶部面板
        JPanel topPanel = createTopPanel();
        add(topPanel, BorderLayout.NORTH);

        // 左侧菜单
        JPanel menuPanel = createMenuPanel();
        add(menuPanel, BorderLayout.WEST);

        // 内容面板
        contentLayout = new CardLayout();
        contentPanel = new JPanel(contentLayout);
        contentPanel.setBackground(Color.WHITE);
        add(contentPanel, BorderLayout.CENTER);

        // 底部状态栏
        JPanel bottomPanel = createBottomPanel();
        add(bottomPanel, BorderLayout.SOUTH);
    }

    private JPanel createTopPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(HotelManagementSystem.getPrimaryColor());
        panel.setBorder(new EmptyBorder(10, 20, 10, 20));

        JLabel titleLabel = new JLabel("酒店房间预定管理系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
        titleLabel.setForeground(Color.WHITE);
        panel.add(titleLabel, BorderLayout.WEST);

        JButton logoutButton = new JButton("退出登录");
        logoutButton.setFont(HotelManagementSystem.getButtonFont());
        logoutButton.setBackground(HotelManagementSystem.getAccentColor());
        logoutButton.setForeground(Color.WHITE);
        logoutButton.setBorder(new EmptyBorder(5, 15, 5, 15));
        logoutButton.setFocusPainted(false);
        logoutButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        logoutButton.addActionListener(e -> {
            int result = JOptionPane.showConfirmDialog(this, "确定要退出登录吗？", "确认", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                mainFrame.showLoginPanel();
            }
        });
        panel.add(logoutButton, BorderLayout.EAST);

        return panel;
    }

    private JPanel createMenuPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setBackground(new Color(44, 62, 80));
        panel.setBorder(new EmptyBorder(20, 10, 20, 10));
        panel.setPreferredSize(new Dimension(200, 0));

        // 菜单按钮
        String[] menuItems = {
                "房间状态管理",
                "前台操作",
                "预定管理",
                "客人信息",
                "历史记录"
        };

        String[] panelNames = {
                "ROOM_STATUS",
                "FRONT_DESK",
                "RESERVATION",
                "GUEST_INFO",
                "HISTORY"
        };

        for (int i = 0; i < menuItems.length; i++) {
            JButton menuButton = createMenuButton(menuItems[i], panelNames[i]);
            panel.add(menuButton);
            panel.add(Box.createVerticalStrut(10));
        }

        return panel;
    }

    private JButton createMenuButton(String text, String panelName) {
        JButton button = new JButton(text);
        button.setFont(HotelManagementSystem.getButtonFont());
        button.setBackground(HotelManagementSystem.getSecondaryColor());
        button.setForeground(Color.WHITE);
        button.setBorder(new EmptyBorder(12, 15, 12, 15));
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
        button.setAlignmentX(Component.CENTER_ALIGNMENT);
        button.setMaximumSize(new Dimension(180, 40));

        button.addActionListener(e -> {
            contentLayout.show(contentPanel, panelName);
            refreshCurrentPanel(panelName);
        });

        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(HotelManagementSystem.getSecondaryColor().brighter());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(HotelManagementSystem.getSecondaryColor());
            }
        });

        return button;
    }

    private JPanel createBottomPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(HotelManagementSystem.getBackgroundColor());
        panel.setBorder(new EmptyBorder(5, 20, 5, 20));

        statusLabel = new JLabel("系统就绪");
        statusLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        statusLabel.setForeground(Color.GRAY);
        panel.add(statusLabel, BorderLayout.WEST);

        JLabel timeLabel = new JLabel(
                new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
        timeLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        timeLabel.setForeground(Color.GRAY);
        panel.add(timeLabel, BorderLayout.EAST);

        // 定时更新时间
        Timer timer = new Timer(1000, e -> {
            timeLabel.setText(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
        });
        timer.start();

        return panel;
    }

    private void initializePanels() {
        // 创建各功能面板
        roomStatusPanel = new RoomStatusPanel();
        frontDeskPanel = new FrontDeskPanel();
        reservationPanel = new ReservationPanel();
        guestInfoPanel = new GuestInfoPanel();
        historyPanel = new HistoryPanel();

        // 添加到内容面板
        contentPanel.add(roomStatusPanel, "ROOM_STATUS");
        contentPanel.add(frontDeskPanel, "FRONT_DESK");
        contentPanel.add(reservationPanel, "RESERVATION");
        contentPanel.add(guestInfoPanel, "GUEST_INFO");
        contentPanel.add(historyPanel, "HISTORY");

        // 默认显示房间状态
        contentLayout.show(contentPanel, "ROOM_STATUS");
        roomStatusPanel.refreshData();
    }

    private void refreshCurrentPanel(String panelName) {
        switch (panelName) {
            case "ROOM_STATUS":
                roomStatusPanel.refreshData();
                break;
            case "FRONT_DESK":
                frontDeskPanel.refreshData();
                break;
            case "RESERVATION":
                reservationPanel.refreshData();
                break;
            case "GUEST_INFO":
                guestInfoPanel.refreshData();
                break;
            case "HISTORY":
                historyPanel.refreshData();
                break;
        }
    }

    public void refreshData() {
        // 刷新当前显示的面板数据
        roomStatusPanel.refreshData();
        updateStatus("数据已刷新");
    }

    public void updateStatus(String message) {
        statusLabel.setText(message);
        Timer timer = new Timer(3000, e -> statusLabel.setText("系统就绪"));
        timer.setRepeats(false);
        timer.start();
    }
}